package com.achievement.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.achievement.domain.dto.ReportAuthorDTO;
import com.achievement.domain.entity.ReportAuthorEntity;
import com.achievement.domain.vo.ReportAuthorVO;
import com.achievement.mapper.ReportAuthorMapper;
import com.achievement.mappings.ReportMapping;
import com.achievement.service.IReportAuthorService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author CNKI
* @description 针对表【biz_research_report_author(科研成果-研究报告列表-作者)】的数据库操作Service实现
* @createDate 2025-02-25 15:58:14
*/
@Service
public class ReportAuthorServiceImpl extends ServiceImpl<ReportAuthorMapper, ReportAuthorEntity>
    implements IReportAuthorService {

    @Autowired
    private ReportMapping mapping;

    /**
     * 根据研究报告id，查询相关作者信息
     *
     * @param id id
     */
    @Override
    public List<ReportAuthorVO> getInfoByReportId(String id) {
        LambdaQueryWrapper<ReportAuthorEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReportAuthorEntity::getResearchReportId,id);
        queryWrapper.orderByAsc(ReportAuthorEntity::getOrderId);

        List<ReportAuthorEntity> list = this.list(queryWrapper);

        List<ReportAuthorVO> voList = list.stream().map(mapping::authorEntity2Vo).collect(Collectors.toList());
        return voList;
    }


    /**
     * 删除作者
     * 根据researchReportId，删除作者信息
     *
     * @param id id
     */
    @Override
    public boolean deleteByReportId(String id) {
        //根据researchReportId，删除作者信息
        LambdaQueryWrapper<ReportAuthorEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReportAuthorEntity::getResearchReportId,id);

        return this.baseMapper.delete(queryWrapper)>0;
    }

    /**
     * 新增作者
     *
     * @param dto List<ReportAuthorDTO>
     */
    @Override
    public boolean add(List<ReportAuthorDTO> dto) {
        List<ReportAuthorEntity> entityList = dto.stream().map(mapping::reportDto2ReportEntity).collect(Collectors.toList());

        if (CollUtil.isNotEmpty(entityList)){
            return this.saveBatch(entityList);
        }

        return true;
    }

    /**
     * 删除作者
     * 根据researchReportId，删除作者信息
     *
     * @param ids List<String>
     */
    @Override
    public boolean deleteByReportIdList(List<String> ids) {
        if (CollUtil.isEmpty(ids)){
            return true;
        }

        //根据researchReportId，删除作者信息
        LambdaQueryWrapper<ReportAuthorEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ReportAuthorEntity::getResearchReportId,ids);

        return this.baseMapper.delete(queryWrapper)>0;
    }
}




